import { IntervalTimeUnit } from "../types/L_Type"
import L_Run from "../utils/L_Run"
import EventGMap from "./EventGMap"
import EventX from "./eventdef/EventX"

/**抽象的事件执行timer队列 类  */
export default class EventExecutor {

  /**事件队列 */
  private eventQue: EventX[]

  private timer: any

  private intervalUnit:IntervalTimeUnit

  private intervalMillSec:number

  private curTimeCount: number

  private maxRun: number

  public isRun:boolean

  constructor( intervalUnit:IntervalTimeUnit,
              maxRun?: number
    ) {
        this.eventQue = []
        this.intervalUnit=intervalUnit
        if(intervalUnit=='SECONDS'){
          this.intervalMillSec=1000
        }else if(intervalUnit=='TENTH_OF_SEC'){
          this.intervalMillSec=100
        }else if(intervalUnit=='HUNDRE_OF_SEC'){
          this.intervalMillSec=10
        }else{
          this.intervalMillSec=1
        }

        this.timer = null
        this.curTimeCount = 0
        this.maxRun = maxRun || -1   //-1代表永久循环
        this.eventQue = []
        this.isRun=false
    }


  public start() {
    //if(this.curTimeCount==undefined){
    console.log('开始运行....')
    this.isRun=true
    let that:EventExecutor=this
    this.timer=L_Run.CreateTimer_L(this.intervalMillSec,()=>{
      that.curTimeCount++
      //console.log(that.secondPreEventQue)
      //事件执行器的自我销毁  注意！在真实环境不存在需要销毁的情况 中心计时器x应该执行到最后
      if (that.maxRun!=-1 && that.maxRun < that.curTimeCount) {
        console.log("销毁计时器,队列数据:",that.eventQue)
        //clearInterval(that.timer)
        L_Run.DestroyTimer_L(that.timer)
        that.isRun=false
      }
      console.log('-----------------------------------------')
      console.log('------',that.intervalUnit,'中心计时器秒数:', that.curTimeCount, "------")
      for (let i = 0; i < that.eventQue.length; i++) {
        let eventx: EventX = that.eventQue[i]
       
        //判断是否需要立即结束的事件  此属性是最高优先级别通过函数endImmd()方法实现  
        if(eventx.execLastedNums<0){
          //在endImmd()方法中已经提前调用了  防止这里调用的延迟问题
         // eventx.end()
          that.eventQue.splice(i, 1)
          i--;
          continue
        }else if(eventx.bePaused){
          //事件是否被暂停了 被暂停了直接跳过  注意只要执行了立即结束 endImmd()  暂停也不好使
          continue
        }
        // //事件第一次执行
        // if(eventx.hasExecNums==0 && eventx.execLastedNums!=-1){
        //   eventx.start()
        // }

        // console.log('事件CTimestamp:',eventx.execCTimestamp)
        if (eventx.execCTimestamp <= that.curTimeCount) {
            // 正常执行扣减
          eventx.hasExecNums++;
          eventx.execLastedNums--
          //防止提前结束的额外执行
          //if(eventx.execLastedNums>=0){
            eventx.run()
          //}
          eventx.execCTimestamp += eventx.intervalVal
          if (eventx.execLastedNums == 0) {
            eventx.end()
            that.eventQue.splice(i, 1)
            i--;
          }
        }
        // console.log(eventx)
      }
    },this.maxRun)
    //this.timer = setInterval(this.executeX, 1000)
    // console.log(this.secondPreEventQue)
    // console.log( this.timer)
    // } else if(this.timer==undefined){
    //   this.timer=setInterval(this.executeX,1000)
    // }

  }



  public  addEvent(e: EventX) {
    console.log('addEvent()', e.eventType)
    e.execCTimestamp = e.intervalVal + this.curTimeCount
    this.eventQue.push(e)
    e.start()
  }



}  